package com.bianguo.android.functiondemo.view.activity;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.bianguo.android.functiondemo.R;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.HashMap;

import butterknife.BindView;

public class GSYVideoActivity extends BaseActivity {

    @BindView(R.id.video_player_view)
    StandardGSYVideoPlayer videoPlayer;
    @BindView(R.id.video_img)
    ImageView videoImg;

//    private static final String url = "https://res.exexm.com/cw_145225549855002";
//    private static final String url = "http://player.youku.com/embed/XMzc5MzExNDkwNA==";
//    private static final String url = "https://media.w3.org/2010/05/sintel/trailer.mp4";
    private static final String url = "http://z-wx.bianguo.com.cn/fzb/data/upload/video/2019-01-17/5c4062cc602a3.mp4";
    private static final String mp4FilePath = "file://android_asset/transparentvideo.mp4";
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Bitmap bitmap = (Bitmap) msg.obj;
                    videoImg.setImageBitmap(bitmap);
                    break;
            }
        }
    };

    @Override
    public int layoutId() {
        return R.layout.activity_gsyvideo;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        videoPlayer.setUp(url, true, "测试视频");
        videoPlayer.startPlayLogic();
//        hideView(videoPlayer.getBackButton());
//        hideView(videoPlayer.getTitleTextView());
        videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resolveFullBtn(videoPlayer);
            }
        });
        new Thread(() -> getNetVideoBitmap(url)).start();
    }

    /**
     * 全屏幕按键处理
     */
    private void resolveFullBtn(final StandardGSYVideoPlayer standardGSYVideoPlayer) {
        standardGSYVideoPlayer.startWindowFullscreen(mContext, true, true);
    }

    private void getNetVideoBitmap(String url) {
        Bitmap bitmap = null;
        MediaMetadataRetriever mRetriever = new MediaMetadataRetriever();
        mRetriever.setDataSource(url, new HashMap<String, String>());
        bitmap = mRetriever.getFrameAtTime();
        mRetriever.release();
        Message message = Message.obtain();
        message.what = 0;
        message.obj = bitmap;
        mHandler.sendMessage(message);
    }

    @Override
    protected void onPause() {
        super.onPause();
        videoPlayer.onVideoPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        videoPlayer.onVideoResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
    }
}
